package xdu.lz.stage5_jucUtils.chapter1_countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 * Created at 2019/10/18 0018 下午 4:29
 */
public class CDL_test3_threadCommunication {

    public static void main(String[] args) {
        CountDownLatch latch = new CountDownLatch(1);
        new Thread(() -> {
            System.out.println("first stage");
            try {
                Thread.sleep(1000);
                //等价于wait for thread2
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("third stage");
        }, "t1").start();

        new Thread(() -> {
            try {
                Thread.sleep(2000);
                System.out.println("asyn prepare data for thread1 to third stage");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                // 等价于notify thread1
                latch.countDown();
            }

        }, "t2").start();


        //测试多个线程都可以调用await
        new Thread(() -> {
            try {
                latch.await();
                System.out.println("test:wait for release ...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "t3").start();
    }
}
